#include "pensetdialog.h"

#include <QDialog>
#include <QColorDialog>

#include <QColor>

PenSetDialog::PenSetDialog(QWidget* parent) : QDialog(parent), ui(new Ui::PenSetDialogClass())
{
    ui->setupUi(this);

    ui->comboBox_Style->clear();//清空
    ui->comboBox_Style->addItem("NoPen");//添加字符串，匹配索引
    ui->comboBox_Style->addItem("SolidLine");
    ui->comboBox_Style->addItem("DashLine");
    ui->comboBox_Style->addItem("DotLine");
    ui->comboBox_Style->addItem("DashDotLine");
    ui->comboBox_Style->addItem("DashDotDotLine");
    ui->comboBox_Style->addItem("CustomDashLine");



    connect(ui->button_color, &QPushButton::clicked, this, [this]() {
        QColor color = QColorDialog::getColor(this->m_pen.color());
        if (color.isValid())
        {
            QString str = QString::asprintf("background color rgb(%d,%d,%d)", color.red(), color.green(), color.blue());
            ui->button_color->setStyleSheet(str);
        }
    });
}

PenSetDialog::~PenSetDialog()
{
    delete ui;
}

QPen PenSetDialog::getPen()
{
    m_pen.setStyle(Qt::PenStyle(ui->comboBox_Style->currentIndex()));
    m_pen.setWidth(ui->spinBox_width->value());
    m_pen.setColor(ui->button_color->palette().color(QPalette::Button));
    return QPen();
}

void PenSetDialog::setPen(const QPen& pen)
{
    this->m_pen = pen;
    ui->spinBox_width->setValue(pen.width());
    ui->comboBox_Style->setCurrentIndex(pen.style());

    ui->button_color->setAutoFillBackground(true);
    QColor color = pen.color();
    QString str = QString::asprintf("background color rgb (%d,%d,%d)", color.red(), color.green(), color.blue());
    ui->button_color->setText(str);
}

QPen PenSetDialog::getPen(const QPen& intPen, bool* ok)
{
    PenSetDialog* dialog = new PenSetDialog;
    dialog->setPen(intPen);
    QPen pen;
    if (dialog->exec() == QDialog::Accepted)
    {
        pen = dialog->getPen();
        *ok = true;
    }
    else
    {
        pen = intPen;
        *ok = false;
    }
    delete dialog;
    return pen;
}
